/**
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-11-24 22:18:20
 * Copyright (c) 2015- joysuch-版权所有
 */

package com.joysuch.wwyt.risk.service.impl;

import com.joysuch.wwyt.bp.repository.BpJobDao;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.common.util.SimpleDateFormatCache;
import com.joysuch.wwyt.core.enums.RiskLevels;
import com.joysuch.wwyt.core.repository.BaseUserDao;
import com.joysuch.wwyt.core.service.BizCodeGeneratorFacade;
import com.joysuch.wwyt.core.support.Context;
import com.joysuch.wwyt.risk.bean.RiskJhaEvaluationBean;
import com.joysuch.wwyt.risk.bean.RiskPointEvaluationBean;
import com.joysuch.wwyt.risk.bean.RiskPointEvaluationItemBean;
import com.joysuch.wwyt.risk.entity.RiskPoint;
import com.joysuch.wwyt.risk.entity.RiskPointEvaluationJha;
import com.joysuch.wwyt.risk.entity.RiskPointEvaluationJhaItem;
import com.joysuch.wwyt.risk.repository.RiskPointEvaluationJhaDao;
import com.joysuch.wwyt.risk.service.RiskPointEvaluationJhaService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class RiskPointEvaluationJhaServiceImpl implements RiskPointEvaluationJhaService {

    @Autowired
    private RiskPointEvaluationJhaDao dao;
    @Autowired
    private BizCodeGeneratorFacade bizCodeGeneratorFacade;
    @Autowired
    private BaseUserDao baseUserDao;
    @Autowired
    private BpJobDao bpJobDao;

    @Override
    public void add(RiskPointEvaluationJha entity) throws Exception {
        dao.save(entity);
    }

    @Override
    public void update(RiskPointEvaluationJha entity) throws Exception {
        RiskPointEvaluationJha content = dao.findById(entity.getId()).get();
        if (content != null) {
            // TODO set nessessary properties
            dao.save(entity);
        }
    }

    @Override
    public Page<RiskPointEvaluationJha> findByPage(Pageable page) {
        return dao.findAll(page);
    }

    @Override
    public void delete(Long id) {
        dao.deleteById(id);
    }

    @Override
    public RiskPointEvaluationJha findById(Long id) {
        return dao.findById(id).orElse(null);
    }

    @Override
    public RiskPointEvaluationJha doEvaluation(RiskPoint point, RiskJhaEvaluationBean evaluationBean) {
        RiskPointEvaluationJha scl = new RiskPointEvaluationJha();
        if (evaluationBean.getAnalyzeTime() == null) {
            scl.setAnalyzeTime(new Date());
        } else {
            scl.setAnalyzeTime(evaluationBean.getAnalyzeTime());
        }
        // 如果为空则设置分析人为当前用户
        if (evaluationBean.getAnalyzeUserId() == null) {
            scl.setBaseUser(baseUserDao.getOne(Context.getCurrentUserId()));
        } else {
            scl.setBaseUser(baseUserDao.getOne(evaluationBean.getAnalyzeUserId()));
        }
        scl.setJobActivity(evaluationBean.getJobActivity());
        scl.setBpJob(evaluationBean.getJobId() == null ? null : bpJobDao.getOne(evaluationBean.getJobId()));
        scl.setInControlNo(evaluationBean.getInControlCode());
        Integer riskLevel = findMaxRiskLevel(evaluationBean);
        scl.setRiskLevel(riskLevel);
        scl.setRiskPointId(point.getId());
        scl.getRiskPointEvaluationJhaItems().addAll(evaluationBean.getItems());
        // 保存对象
        dao.save(scl);
        return scl;
    }

    @Override
    public ResultBean getById(Long id) {
        RiskPointEvaluationJha jha = dao.findById(id).orElseThrow(() -> new IllegalArgumentException("没有评估记录"));
        RiskPointEvaluationBean bean = new RiskPointEvaluationBean();
        bean.setId(jha.getId());
        bean.setAnalyzeUserId(jha.getBaseUser().getId());
        bean.setAnalyzeTime(SimpleDateFormatCache.getYmdhms().format(jha.getAnalyzeTime()));
        bean.setAnalyzeUserName(jha.getBaseUser().getRealName());
        bean.setRiskLevel(jha.getRiskLevel());
        bean.setInControlCode(jha.getInControlNo());
        bean.setJobActivity(jha.getJobActivity());
        bean.setRiskLevelName(RiskLevels.findRiskLevelName(jha.getRiskLevel()));
        //详情
        List<RiskPointEvaluationItemBean> list = new ArrayList<>(jha.getRiskPointEvaluationJhaItems().size());
        for (RiskPointEvaluationJhaItem item : jha.getRiskPointEvaluationJhaItems()) {
            RiskPointEvaluationItemBean b = new RiskPointEvaluationItemBean();
            BeanUtils.copyProperties(item, b);
            b.setRiskLevelName(RiskLevels.findRiskLevelName(item.getRiskLevel()));
            list.add(b);
        }
        list.sort(Comparator.comparing(RiskPointEvaluationItemBean::getId));
        bean.setItems(list);
        return ResultBean.success(bean);
    }

    private Integer findMaxRiskLevel(RiskJhaEvaluationBean evaluationBean) {
        Integer level = Integer.MAX_VALUE;
        // if (evaluationBean.getRiskLevel() != null) {
        // level = evaluationBean.getRiskLevel();
        // }
        List<RiskPointEvaluationJhaItem> items = evaluationBean.getItems();
        for (RiskPointEvaluationJhaItem t : items) {
            if (t.getRiskLevel() != null && level > t.getRiskLevel()) {
                level = t.getRiskLevel();
            }
        }
        return level == Integer.MAX_VALUE ? null : level;
    }

}
